<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>接口请求概览</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <script src="js/jquery.js"></script>
    <script src="js/laydate/laydate.js"></script>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<style>
    .top-panel {
        border: 1px solid #eceff9;
        border-radius: 5px;
        text-align: center;
    }
    .top-panel > .layui-card-body{
        height: 60px;
    }
    .top-panel-number{
        line-height:60px;
        font-size: 30px;
        border-right:1px solid #eceff9;
    }
    .top-panel-tips{
        line-height:30px;
        font-size: 12px
    }
    .panel-box {padding:5px 15px}
    .main_btn > p {height:40px;}
</style>
<body>


    <div class="panel-box">

            <legend>接口监控检索</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="form-inline" action="" id="query_form">
                    <input type="hidden" name="type" value="interface">

                    <div class="form-group">
                        <select name="address" id="address" class="form-control">
                            <option value="">所有服务器</option>
                            {list $ip_list}
                            <option value="{$val}">{$val}</option>
                            {/list}
                        </select>
                    </div>
                    <div class="form-group">
                        <select name="date" id="date" class="form-control">
                            {list $date_list}
                            <option value="{$val}"<?=$val==$date?' selected':''?>>{$val}</option>
                            {/list}
                        </select>
                    </div>
                    <div class="form-group">
                        <select name="module" id="module_list" class="form-control">
                            <option value="">模块</option>
                            {list $module_list}
                            <option value="{$key}"<?=$key==$module?' selected':''?>>{$val}</option>
                            {/list}
                        </select>
                    </div>
                    <div class="form-group">
                        <select name="name" id="depth_list" class="form-control">
                            <option value="">统计项</option>
                        </select>
                    </div>
                    <div class="form-group">
                        <button type="submit" class="btn btn-default">查询</button>
                    </div>
                </form>
            </div>
    </div>
    <div class="panel-box">
        {if $fail_address}
        <blockquote class="layui-elem-quote" style="border-left-color: #FF5722;display: none;" id="fail_address">
            <strong>无法从以下数据源获取数据:</strong><?=implode(', ', $fail_address)?>
        </blockquote>
        {/if}
        <div class="row">
            <div class="col-md-6">
                <div class="panel panel-primary">
                    <div class="panel-heading">总请求</div>
                    <div class="panel-body">
                        <div class="label label-success" id="total_count"><?=$count['total_count']?></div>
                        成功 <a style="color: #1aa094" id="suc_count"><?=$count['suc_count']?></a>
                        失败 <a style="color: #bd3004" id="fail_count"><?=$count['fail_count']?></a>
                        慢请求 <a style="color: #bd3004" id="slow_times"><?=$count['slow_times']?></a>
                        最大耗时 <a style="color: #bd3004" id="max_cost_time"><?=$count['max_cost_time']?></a>
                        最小耗时 <a style="color: #bd3004" id="min_cost_time"><?=$count['min_cost_time']?></a>
                    </div>
                </div>
            </div>
            <div class="col-md-6">
                <div class="panel panel-primary">
                    <div class="panel-heading">可用性</div>
                    <div class="panel-body">
                        <div class="label label-success">成功 <b id="suc_rate"><?=$count['suc_rate']?>%</b></div>
                        <div class="label label-danger">不可用 <b id="no_suc_rate"><?=round(100-$count['suc_rate'],4)?>%</b></div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <div class="panel-box">
        <div class="panel panel-success">
            <div class="panel-heading"><i class="fa fa-line-chart icon"></i>请求量曲线</div>
            <div class="panel-body">
                <div id="req-records" style="width: 100%;min-height:300px"></div>
            </div>
        </div>
        <div class="panel panel-info">
            <div class="panel-heading"><i class="fa fa-line-chart icon"></i>请求耗时曲线</div>
            <div class="panel-body">
                <div id="cost-records" style="width: 100%;min-height:200px"></div>
            </div>
        </div>

        <table class="table">
            <thead>
            <tr>
                <th>时间</th>
                <th>调用总数</th>
                <th>慢请求数</th>
                <th>最大耗时</th>
                <th>最小耗时</th>
                <th>平均耗时</th>
                <th>成功调用总数</th>
                <th>成功平均耗时</th>
                <th>失败调用总数</th>
                <th>失败平均耗时</th>
                <th>成功率</th>
            </tr>
            </thead>
            <tbody id="query_ret">
            {if $data}
            {list $data $v}
            <tr>
                <td>{$v['time']}</td>
                <td>{$v['total_count']}</td>
                <td>{$v['slow_times']}</td>
                <td>{$v['max_cost_time']}</td>
                <td>{$v['min_cost_time']}</td>
                <td>{$v['total_avg_time']}</td>
                <td>{$v['suc_count']}</td>
                <td>{$v['suc_avg_time']}</td>
                <td>{$v['fail_count']}</td>
                <td>{$v['fail_avg_time']}</td>
                <td>{$v['precent']}</td>
            </tr>
            {/list}
            {else}
            <tr>
                <td colspan="100%">暂无数据</td>
            </tr>
            {/if}
            </tbody>
        </table>
    </div>

<script src="https://cdn.jsdelivr.net/npm/echarts@5.0.0/dist/echarts.min.js"></script>
<script>
    var echartsReq = echarts.init(document.getElementById('req-records'));
    var echartsCost = echarts.init(document.getElementById('cost-records'));

    var costOption = {
        tooltip: {
            trigger: 'axis'
        },
        legend: {
            data:['成功平均耗时','失败平均耗时','最大耗时','最小耗时'],
        },
        grid: {
            left: '3%',
            right: '2%',
            bottom: '3%', top:'40px',
            containLabel: true
        },
        toolbox: {
            feature: {
                saveAsImage: {}
            }
        },
        xAxis: {
            type: 'category',
            boundaryGap: false,
            data: {*toJson($xAxis)} //['周一','周二','周三','周四','周五','周六','周日']
        },
        yAxis: {
            name: '平均耗时(单位：秒)',
            type: 'value'
        },
        series: [
            {
                name:'成功平均耗时',
                type:'line',
                data:{*toJson($ok_cost_data)} //[120, 132, 101, 134, 90, 230, 210]
            },
            {
                name:'失败平均耗时',
                type:'line',
                data:{*toJson($fail_cost_data)} //[220, 182, 191, 234, 290, 330, 310]
            },
            {
                name:'最大耗时',
                type:'line',
                data:{*toJson($max_cost_data)}
            },
            {
                name:'最小耗时',
                type:'line',
                data:{*toJson($min_cost_data)}
            }
        ]
    };
    var reqOption = {
        tooltip: {
            trigger: 'axis'
        },
        legend: {
            data:['成功曲线','失败曲线','慢请求']
        },
        grid: {
            left: '3%',
            right: '2%',
            bottom: '3%', top:'40px',
            containLabel: true
        },
        toolbox: {
            feature: {
                saveAsImage: {}
            }
        },
        xAxis: {
            type: 'category',
            boundaryGap: false,
            data: {*toJson($xAxis)} //['周一','周二','周三','周四','周五','周六','周日']
        },
        yAxis: {
            name: '请求量(次/5分钟)',
            type: 'value'
        },
        series: [
            {
                name:'成功曲线',
                type:'line',
                data:{*toJson($ok_data)} //[120, 132, 101, 134, 90, 230, 210]
            },
            {
                name:'失败曲线',
                type:'line',
                data:{*toJson($fail_data)} //[220, 182, 191, 234, 290, 330, 310]
            },
            {
                name:'慢请求',
                type:'line',
                data:{*toJson($slow_data)}
            }
        ]
    };
    /**
     * 报表功能
     */
    echartsReq.setOption(reqOption);
    echartsCost.setOption(costOption);

    // echarts 窗口缩放自适应
    window.onresize = function(){
        echartsReq.resize();
        echartsCost.resize();
    }

    var depth_list = $('#depth_list');
    $('#module_list').change(function(){
        var val = $(this).val();
        if(val!=''){
            depth_list.attr('disabled','disabled');
            $.getJSON('/index/path',{address:$('#address').val(),date:$('#date').val(),type:'interface',module:val}, function(json){
                depth_list.removeAttr('disabled');
                if(json.code!=0){
                    alert(json.msg);
                    return false;
                }
                depth_list.children('option').remove();
                depth_list.append('<option value="">统计项</option>');
                for(name in json.data){
                    var val = json.data[name];
                    depth_list.append('<option value="'+ name +'">'+ name + ' (' + val +')</option>');
                }
            });
        }
    });

    var queryRet = $('#query_ret');
    var buttonSubmit = $('button[name="submit"]');
    $('#query_form').submit(function () {
        buttonSubmit.attr('disabled','disabled');
        $.getJSON('/index/info2?json=1', $(this).serialize(), function(json){
            buttonSubmit.removeAttr('disabled');
            if(json.code!=0){
                alert(json.msg);
                return false;
            }
            $('#total_count').html(json.data.count.total_count);
            $('#suc_count').html(json.data.count.suc_count);
            $('#fail_count').html(json.data.count.fail_count);

            $('#slow_times').html(json.data.count.slow_times);
            $('#max_cost_time').html(json.data.count.max_cost_time);
            $('#min_cost_time').html(json.data.count.min_cost_time);

            $('#suc_rate').html(json.data.count.suc_rate);
            $('#no_suc_rate').html((100-json.data.count.suc_rate).toFixed(4));
            if(json.data.fail_address.length>0){
                $('#fail_address').html('<strong>无法从以下数据源获取数据:</strong>'+ json.data.fail_address.join(','));
                $('#fail_address').show();
            }

            queryRet.html('');
            for(i in json.data['data']){
                var $v = json.data['data'][i];
                queryRet.append('<tr>\
                    <td>'+ $v['time'] +'</td>\
                    <td>'+ $v['total_count'] +'</td>\
                    <td>'+ $v['slow_times'] +'</td>\
                    <td>'+ $v['max_cost_time'] +'</td>\
                    <td>'+ $v['min_cost_time'] +'</td>\
                    <td>'+ $v['total_avg_time'] +'</td>\
                    <td>'+ $v['suc_count'] +'</td>\
                    <td>'+ $v['suc_avg_time'] +'</td>\
                    <td>'+ $v['fail_count'] +'</td>\
                    <td>'+ $v['fail_avg_time'] +'</td>\
                    <td>'+ $v['precent'] +'</td>\
                    </tr>')
            }

            costOption.xAxis.data = json.data['xAxis'];
            costOption.series = [
                {
                    name:'成功平均耗时',
                    type:'line',
                    data:json.data['ok_cost_data'] //[120, 132, 101, 134, 90, 230, 210]
                },
                {
                    name:'失败平均耗时',
                    type:'line',
                    data:json.data['fail_cost_data'] //[220, 182, 191, 234, 290, 330, 310]
                },
                {
                    name:'最大耗时',
                    type:'line',
                    data:json.data['max_cost_data']
                },
                {
                    name:'最小耗时',
                    type:'line',
                    data:json.data['min_cost_data']
                }
            ];

            reqOption.xAxis.data = json.data['xAxis'];
            reqOption.series = [
                {
                    name:'成功曲线',
                    type:'line',
                    data:json.data['ok_data'] //[120, 132, 101, 134, 90, 230, 210]
                },
                {
                    name:'失败曲线',
                    type:'line',
                    data:json.data['fail_data'] //[220, 182, 191, 234, 290, 330, 310]
                },
                {
                    name:'慢请求',
                    type:'line',
                    data:json.data['slow_data']
                }
            ];

            echartsReq.setOption(reqOption);
            echartsCost.setOption(costOption);
        });

        return false;
    });
    $('#module_list').trigger('change');
</script>
</body>
</html>